<?php

namespace Xrfg\Core\Trading\Account\Observers;

use Carbon\Carbon;
use Xrfg\Core\Common\Services\Proof;
use Xrfg\Core\Trading\Account\Models\Account;
use Xrfg\Core\Trading\Account\Models\AccountCapitalflow;

class AccountObserver
{
    public function updated(Account $account)
    {
        if ($account->availableChange || $account->disabledChange) {
            $log = new AccountCapitalflow();
            $log->coin_id = $account->coin_id;
            $log->user_id = $account->user_id;
            $log->available_before = $account->getOriginal('available');
            $log->available_number = $account->availableChange ?: 0;
            $log->available_after = $account->available;
            $log->disabled_before = $account->getOriginal('disabled');
            $log->disabled_number = $account->disabledChange ?: 0;
            $log->disabled_after = $account->disabled;
            $log->certificate = Proof::encode($account->proofId[0], $account->proofId[1], $account->proofId[2]);
            $log->proof_id = $account->proofId[0];
            $log->class_id = $account->proofId[1];
            $log->op_id = $account->proofId[2];
            $log->version = $account->version;
            $log->created_at = Carbon::now();
            $log->updated_at = Carbon::now();
            $log->save();
        }
    }
}
